<?php

use console\db\Migration;
use mdm\admin\components\Configs;

/**
 * 初始化管理员数据
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m200104_091356_d_adminer extends Migration {

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $rows = $this->getData('adminer.php');
        $tablename = Configs::instance()->userTable;
        foreach ($rows as $row) {
            $this->insert($tablename, [
                'username' => $row['username'],
                'mobile' => $row['mobile'],
                'realname' => $row['realname'],
                'password_hash' => Yii::$app->security->generatePasswordHash($row['password']),
                'auth_key' => Yii::$app->security->generateRandomString(),
                'created_at' => time(),
                'updated_at' => time(),
            ]);
        }
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        $tablename = Configs::instance()->userTable;
        $this->delete($tablename, [
            '>', 'id', 1
        ]);
        $this->resetAutoIncrement($tablename);
    }

}
